﻿using IrrKlang;
using IrrlichtLime;
using IrrlichtLime.Core;
using IrrlichtLime.Video;
using Iso.Common;
using Iso.Core;
using LuaInterface;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using Timed;

namespace Game
{
    public class Program
    {
        public static Engine Engine { get; private set; }
        static void Main(string[] args)
        {
            Engine = new Iso.Common.Engine();

            // Carica il file di configurazioni
            IrrlichtCreationParameters param = new IrrlichtCreationParameters();
            param.WindowSize = new Dimension2Di(int.Parse(ConfigurationManager.AppSettings["width"]),
                int.Parse(ConfigurationManager.AppSettings["height"]));
            param.DriverType = DriverType.Direct3D9;
            param.AntiAliasing = byte.Parse(ConfigurationManager.AppSettings["antialiasing"]);
            param.Fullscreen = bool.Parse(ConfigurationManager.AppSettings["fullscreen"]);
            param.BitsPerPixel = 32;
            Engine.baseData = ConfigurationManager.AppSettings["baseData"];

            // Crea la device
            Engine.device = IrrlichtDevice.CreateDevice(param);
            Engine.device.CursorControl.Visible = false;
            Engine.soundEngine = new ISoundEngine();
            Engine.soundEngine.SoundVolume = float.Parse(ConfigurationManager.AppSettings["volume"]);
            Engine.sounds = new Dictionary<string, ISound>();
            Engine.lua = new Lua();
            Engine.globalVariables = new Dictionary<string, object>();

            // Controlla che non ci siano stati errori
            if (Engine.device != null)
            {
                Keyboard.Register(Engine.device);
                LuaHook.Register();
                Game.Plugin.CustomLua.Register();
                Utility.PreloadGraphics();
                Engine.timeline = new Timeline();

                // Start LUA Script for game start
                Engine.lua.DoFile(Engine.baseData + "scripts\\boot.lua");

                Engine.then = Engine.device.Timer.Time;
                while (Engine.device.Run())
                {
                    // Change scene
                    if (Engine.nextScene != null)
                    {
                        if (Engine.currentScene != null)
                            Engine.UnloadScene();
                        Engine.currentScene = Engine.nextScene;
                        Engine.currentScene.Load();
                        Engine.nextScene = null;
                    }

                    Engine.timeline.Check(Engine.device.Timer.RealTime);

                    // Update and Draw scene
                    Engine.currentScene.Update();
                    Engine.currentScene.Draw();

                    Engine.now = Engine.device.Timer.Time;
                    Engine.then = Engine.now;

                    Engine.soundEngine.Update();
                }

                // Unload all
                Engine.UnloadScene();
                Engine.device.Drop();
                Engine.soundEngine.Dispose();
                Engine.lua.Close();
            }
            else
                System.Windows.Forms.MessageBox.Show("Cannot initialize DirectX9 device!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }
    }
}
